package com.mycat.manager.common;

import com.mycat.dao.entity.auth.Admin;
import com.mycat.manager.content.shiro.CustomerUserRealm;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;

import java.util.List;

/**
 * @author roger.
 * @email luojie@yzworld.cn
 * @date 2016/10/8
 */
public class BaseController {

    /**
     * 获取登录用户id
     *
     * @return ID
     * @throws LoginTimeOutException
     */
    public Integer getLoginId() throws LoginTimeOutException {
        Session session = SecurityUtils.getSubject().getSession();
        Object loginId = session.getAttribute(CustomerUserRealm.SESSION_LOGIN_USER_ID);
        if (null == loginId) {
            throw new LoginTimeOutException();
        }
        return (Integer) loginId;
    }

    /**
     * 获取登录用户
     *
     * @return Admin
     * @throws LoginTimeOutException
     */
    public Admin getLoginUser() throws LoginTimeOutException {
        Session session = SecurityUtils.getSubject().getSession();
        Object admin = session.getAttribute(CustomerUserRealm.SESSION_LOGIN_USER);
        if (null == admin) {
            throw new LoginTimeOutException();
        }
        return (Admin) admin;
    }

    /**
     * 获取登录用户所属角色集合ID
     *
     * @return List<Integer>
     * @throws LoginTimeOutException
     */
    public List<Integer> getLoginRoleIds() throws LoginTimeOutException {
        Session session = SecurityUtils.getSubject().getSession();
        Object roleIds = session.getAttribute(CustomerUserRealm.SESSION_LOGIN_USER_ROLE_IDS);
        if (null == roleIds) {
            throw new LoginTimeOutException();
        }
        return (List<Integer>) roleIds;
    }

    public void openMenu() throws LoginTimeOutException{

    }
}
